require( [ "dojo/on", "dojo/dom-attr", "dojo/number", "dojo/dom", "dojo/dom-class",
	   "dojo/query", "dojo/_base/fx", "dojo/dom-style", "dojo/fx",
	   'dojo/NodeList-manipulate', 'dojo/NodeList-dom', 'dojo/domReady!' ],
	 function(on, domAttr, dNumber, dom, domClass, query, fx, domStyle, coreFx) {
	     
	     var calculateLO = function(if1, if2) {
		 var fl, fh, lofreq, cabbf, detf;
		 cabbf = [];
		 fl = fh = if1;
		 fl = (if1 < if2) ? if1 : if2;
		 fh = (if1 < if2) ? if2 : if1;
		 detf = (if1 <= if2) ? 0 : 1;
		 adetf = (if1 <= if2) ? 1 : 0;

		 var df = fh - fl;
		 var offset = (df >= 5000) ? 10928 : 10000;
		 var lo_l = 12100;
		 var lo_h = 15200;
		 var sideband = 0;

		 if (fh <= 40672) {
		     // LSB
		     if ((fl + offset) < (lo_h * 3)) {
			 lofreq = fl + offset;
			 cabbf[detf] = offset;
			 cabbf[adetf] = offset - (fh - fl);
		     } else {
			 lofreq = lo_h * 3;
			 cabbf[detf] = (fl - 3 * lofreq) / -1;
			 cabbf[adetf] = (fh - 3 * lofreq) / -1;
		     }
		     sideband = -1;
		 } else {
		     // USB
		     if ((fh - offset) > (lo_l * 3)) {
			 lofreq = fh - offset;
			 cabbf[adetf] = offset;
			 cabbf[detf] = offset - (fh - fl);
		     } else {
			 lofreq = lo_l * 3;
			 cabbf[adetf] = (fl - 3 * lofreq);
			 cabbf[detf] = (fh - 3 * lofreq);
		     }
		     sideband = 1;
		 }
		 
		 return {
		     'site-synth': dNumber.round((lofreq / 3), 3),
		     'cabb': cabbf,
		     'sideband': sideband
		 };
	     };

	     var lo2sky = function(synth, cabb, sideband) {
		 return (3 * synth + sideband * cabb);
	     };

	     var showAlert = function(alertId) {
		 if (dom.byId(alertId)) {
		     domClass.add(alertId, 'md-show');
		 }
	     };

	     var panelShown = false;
	     var shownId = null;

	     var showPanel = function(panelId) {
		 // Set up an animation chain.
		 var animChain = [];
		 var animDuration = 100;
		 // Hide the current panel if necessary.
		 if (panelShown && dom.byId(shownId)) {
		     animChain.push(fx.fadeOut({
			 'node': shownId,
			 'duration': animDuration,
			 'onEnd': function() {
			     domClass.add(shownId, 'invisible');
			     shownId = null;
			     panelShown = false;
			 }
		     }));
		 }
		 // Display the panel.
		 if (panelId && dom.byId(panelId)) {
		     animChain.push(fx.fadeIn({
			 'node': panelId,
			 'duration': animDuration,
			 'beforeBegin': function() {
			     domClass.remove(panelId, 'invisible');
			     shownId = panelId;
			     panelShown = true;
			 }
		     }));
		 }

		 // Trigger the animation.
		 if (animChain.length > 0) {
		     coreFx.chain(animChain).play();
		 }

		 return;
	     };

	     on(dom.byId('button-check'), 'click', function(e) {
		 // Get the frequencies and check them.
		 var if1 = parseInt(domAttr.get('frequency-if1', 'value'));
		 var if2 = parseInt(domAttr.get('frequency-if2', 'value'));
		 if (if1 < 30000 || if1 > 50000 ||
		     if2 < 30000 || if2 > 50000) {
		     showAlert('modal-error-non7mm');
		     return;
		 }
		 var correlator = query('[name="data-correlator"]:checked').val();
		 if (correlator === 'CABB' &&
		     Math.abs(if2 - if1) > 6000) {
		     showAlert('modal-error-cabb-widespacing');
		     return;
		 }
		 if (correlator !== 'CABB') {
		     // Don't have calculations for pre-CABB yet.
		     showAlert('modal-error-temporary');
		     return;
		 }
		 if ((if1 < 40672 && if2 > 40672) ||
		     (if1 > 40672 && if2 < 40672)) {
		     showAlert('modal-error-sideband');
		     return;
		 }
		 // Calculate the CABB LO now.
		 var loFrequency = calculateLO(if1, if2);
		 var cIF1 = lo2sky(loFrequency['site-synth'], loFrequency.cabb[0],
				   loFrequency.sideband);
		 var cIF2 = lo2sky(loFrequency['site-synth'], loFrequency.cabb[1],
				   loFrequency.sideband);
		 if ((cIF1 - if1) !== 0 ||
		     (cIF2 - if2) !== 0) {
		     // Fill in and show the "you've got problems" panel.
		     domAttr.set('if1-real-frequency', 'innerHTML',
				 dNumber.round(cIF1, 3));
		     domAttr.set('if2-real-frequency', 'innerHTML',
				 dNumber.round(cIF2, 3));
		     domAttr.set('if1-offset', 'innerHTML',
				 dNumber.round(((cIF1 - if1) * 1000), 0));
		     domAttr.set('if2-offset', 'innerHTML',
				 dNumber.round(((cIF2 - if2) * 1000), 0));
		     showPanel('result-affected');
		 } else {
		     // Show the "everything's alright" result.
		     showPanel('result-unaffected');
		 }
	     });

	     query('.md-close').on('click', function(e) {
		 query('.md-modal').removeClass('md-show');
	     });

	 });
